package sanphone.com.runningschool.Fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import sanphone.com.runningschool.ForceOffline;
import sanphone.com.runningschool.InterfaceLocation;
import sanphone.com.runningschool.MainActivity;
import sanphone.com.runningschool.RequestHttp.RequestHttp;
import sanphone.com.runningschool.Saying.SayingActivity;
import sanphone.com.runningschool.Saying.SayingListViewAdapter;
import sanphone.com.runningschool.R;
import sanphone.com.runningschool.entity.Question;

import static android.content.Context.MODE_PRIVATE;


public class SayingFragment extends Fragment {
    private SwipeRefreshLayout refreshLayout;
    private List<Question> questions=null;
    private ListView info;
    private SayingListViewAdapter adapter;
    private SharedPreferences sprf;
    private TextView emptyText;
    private String id;
    private ForceOffline offline;


    public static SayingFragment newInstance() {
        SayingFragment fragment = new SayingFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.saying_fragment, null);
        sprf=getActivity().getSharedPreferences("data",MODE_PRIVATE);
        id = sprf.getString("schoolId", null);
        offline = new ForceOffline(id,getActivity());
        Toolbar toolbar = view.findViewById(R.id.toolbar1);
        toolbar.inflateMenu(R.menu.saying_menu);
        toolbar.setTitle("");
        ((MainActivity) getActivity()).setSupportActionBar(toolbar);
        setHasOptionsMenu(true);
        info = view.findViewById(R.id.saying_info_listview);
        emptyText=view.findViewById(R.id.empty_text);
        refreshLayout = view.findViewById(R.id.saying_refresh);
        refreshLayout.setColorSchemeResources(R.color.colorPrimary);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        // TODO: 2018/8/7 设置自动刷新
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(true);
                refresh();
            }
        });

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        offline.check();

    }

    public void getDataFromInternet()  {
        ExecutorService executorService= Executors.newSingleThreadExecutor();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                RequestHttp requestHttp = new RequestHttp(InterfaceLocation.getLocation() + "getAllUserHelp",getActivity());
                try {
                   String request = requestHttp.get();
                    Log.e("saying", request);
                    String  data = new JSONObject(request).getString("data");
                    Gson gson = new Gson();
                    questions = gson.fromJson(data, new TypeToken<List<Question>>() {}.getType());

                    if(questions.size()==0){
                        emptyText.setVisibility(View.VISIBLE);
                    }else {
                        Collections.reverse(questions);
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                adapter = new SayingListViewAdapter(getActivity(), questions);
                                info.setAdapter(adapter);
                            }
                        });
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
        executorService.shutdown();

    }
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.saying_menu, menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.say_something:
                Intent intent = new Intent(getContext(), SayingActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
        return true;
    }

    // TODO: 2018/8/7 自定义刷新函数
    private void refresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                       getDataFromInternet();
                       offline.check();
                        refreshLayout.setRefreshing(false);
                    }
                });
            }
        }).start();
    }
}
